<?php defined('SYSPATH') or die('No direct script access.');
/**
 * Pumpio HTTP Post Controller
 *
 * PHP version 5
 * LICENSE: This source file is subject to AGPL license 
 * that is available through the world-wide-web at the following URI:
 * http://www.gnu.org/licenses/agpl.html
 * @author	   Omar Vega Ramos <ovruni@gnu.org.pe> 
 * @copyright      (c) 2012 - 2016 Omar Vega Ramos
 * @license	   http://www.gnu.org/licenses/agpl.html GNU Affero General Public License (AGPL) 
 */

class Pumpio_Controller extends Controller {
  
  public function __construct()
  {
    parent::__construct();
    
    // If user doesn't have access, redirect to login
    if (!$this->auth->has_permission('admin_ui')) {
      url::redirect(url::site('login'));
    }
  }
  
  public function index()
  {
    // Retrieve Current Settings
    $pumpio = ORM::factory('pumpio', 1);
    
    if ((($pumpio->consumer_key == '') OR ($pumpio->consumer_secret == '')) AND ($pumpio->pumpio_host != '')) {
      $clientdata = $this->_registerclient($pumpio->pumpio_host);
      
      if ($clientdata == FALSE) {
	echo 'Client registration:  ERROR.  Refresh the page or try again later.';
	return;
      }
      
      $pumpio->consumer_key = $clientdata->client_id;
      $pumpio->consumer_secret = $clientdata->client_secret;
      $pumpio->save();
      echo 'Client registration:  OK.<br>';
    }
    
    if (($pumpio->consumer_key == '') OR ($pumpio->consumer_secret == '')) {
      echo 'Need set pump.io host and pump.io username.';
      header('Refresh: 5; URL='.url::site('admin/pumpio_settings'));
      return;
    }
    
    $client = new Oauth_client_class;
    $client->debug = 1;
    $client->server = '';
    $client->oauth_version = '1.0a';
    $client->request_token_url = 'https://'.$pumpio->pumpio_host.'/oauth/request_token';
    $client->dialog_url = 'https://'.$pumpio->pumpio_host.'/oauth/authorize';
    $client->access_token_url = 'https://'.$pumpio->pumpio_host.'/oauth/access_token';
    $client->url_parameters = FALSE;
    $client->authorization_header = TRUE;
    $client->redirect_uri = url::site('pumpio');
    $client->client_id = $pumpio->consumer_key;
    $client->client_secret = $pumpio->consumer_secret;
    
    if (($success = $client->Initialize())) {
      if (($success = $client->Process())) {
	if (strlen($client->access_token)) {
	  $pumpio->oauth_token = $client->access_token;
	  $pumpio->oauth_token_secret = $client->access_token_secret;
	  $pumpio->save();
	}
      }
      
      $success = $client->Finalize($success);
    }
    
    if ($client->exit)
      $output = 'Could not connect to pump.io . Refresh the page or try again later.';
    
    if ($success) {
      $output = 'You are now authenticated to pump.io';
    }
    
    echo $output;
    header('Refresh: 5; URL='.url::site('admin/pumpio_settings'));
  }
  
  private function _registerclient($host)
  {
    // Retrieve Current Settings
    $admin = ORM::factory('user', 1);
    
    $endpoint = 'https://'.$host.'/api/client/register';
    
    $params = array();
    
    $params['type'] = 'client_associate';
    $params['contacts'] = $admin->email;
    $params['application_type'] = 'web';
    $params['application_name'] = 'Ushahidi';
    $params['redirect_uris'] = url::site('pumpio');
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_USERAGENT, 'Ushahidi');
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_URL, $endpoint);
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
    
    $response = curl_exec($ch);
    
    if (!curl_errno($ch)) {
      $data = json_decode($response);
    } else {
      $data = FALSE;
    }
    
    curl_close($ch);
    return $data;
  }
}